package org.lmw.wechat.ui;

import org.lmw.wechat.App;
import org.lmw.wechat.BaseActivity;
import org.lmw.wechat.R;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ViewSwitcher;

public class SignActivity extends BaseActivity  implements OnClickListener, Handler.Callback {
	Handler hd;
	ViewSwitcher switcher;
	Button codeBtn;
	EditText phone,pwd;
	Button btn_reg,btn_login;
	int a = 120;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_sign);
		initView();
	}

	private void initView() {
		hd = new Handler(this);
		switcher = (ViewSwitcher) findViewById(R.id.viewSwitcher1);
		codeBtn = (Button) findViewById(R.id.getcode);
		phone = (EditText) findViewById(R.id.phone_edit);
		codeBtn.setOnClickListener(this);
		btn_login=(Button)findViewById(R.id.submit_login);
		btn_reg=(Button)findViewById(R.id.submit_reg);
		btn_login.setOnClickListener(this);
		btn_reg.setOnClickListener(this);
	}

	@Override
	public boolean handleMessage(Message msg) {

		return false;
	}

	public void onClick(View v) {
		if (v == codeBtn) {
			if (phone.getText().toString().trim().length() < 11) {
				Toast.makeText(getApplicationContext(), "请填写手机号", Toast.LENGTH_SHORT).show();
				return;
			}
			sendPost(phone.getText().toString().trim());
			new TimeCount(1000 * 20, 1000).start();
		}
		if(v==btn_reg){
			
			switcher.setDisplayedChild(0);
		}
		if(v==btn_login){
//			startActivity(new Intent(getApplicationContext(), Act_Main.class));
//			finish();
			App.LOGINED=true;
			setResult(RESULT_OK);
			finish();
		}
	}

	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);// 总时长,时间间隔
		}
		@Override
		public void onFinish() {// 计时完毕
			codeBtn.setText("重新获取");
			codeBtn.setEnabled(true);
		}
		@Override
		public void onTick(long millisUntilFinished) {// 计时中
			codeBtn.setEnabled(false);
			codeBtn.setText(millisUntilFinished / 1000 + "s秒重新获取");
		}
	}

	public void sendPost(String phoneNo) {
		
	}

	public void switchView(View v) {
		if (v.getId() == R.id.toreg)
			switcher.setDisplayedChild(1);
		if (v.getId() == R.id.tologin)
			switcher.setDisplayedChild(0);
	 }
}